package com.herohead;

import java.io.IOException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import com.herohead.redis.GetRedisServer;


public class WorkerThread implements Runnable {

	public static String getScore(Jedis jd,String hash, String key) {
		return jd.hget(hash, key);
	}
	private final String command;
	private final String queID;



	public WorkerThread(String s,String queIDs){
		this.command=s;
		this.queID=queIDs;
	}

	private void processCommand() throws InterruptedException, IOException {
		GetRedisServer grs = new GetRedisServer();
		Jedis jd = new Jedis(grs.getRedisHost());
		try {
			String threadStartTime = (Misc.getTimestamp());
			System.out.println(threadStartTime + " Starting : "+ command);


			//String[] ssidInCommand = command.split("-S")[1].split(" ");
			String[] ssidInCommand = command.split("-S")[1].trim().split(" ");


			Long processID = jd.incr("thread:cnt");
			jd.hset("prod:threads:running:processID",String.valueOf(queID), getScore(jd,"prod:threads:queued:processID",String.valueOf(queID)));
			jd.hdel("prod:threads:queued:processID",String.valueOf(queID));
			jd.rpush("prod:threads:running", String.valueOf(processID) + "|" + threadStartTime + "|" + command);
			jd.hdel("prod:threads:queued",queID);
			String tempkey = String.valueOf(processID) + "|" + threadStartTime + "|" + command;
			//jd.close();
			// Run command
			Process p = Runtime.getRuntime().exec("cmd /c start /wait " + command);


			System.out.println("Waiting for clone to finish ...");
			p.waitFor();

			String threadEndTime = (Misc.getTimestamp());

			if (p.exitValue() == 0) {
				Jedis jdpl = new Jedis(grs.getRedisHost());
				Transaction tx = jdpl.multi();
				for (String ssid : ssidInCommand) {
					tx.srem("s_prod:ssid:not:cloned", ssid);
				}
				tx.exec();
				jdpl.close();

				jd.hset("prod:threads:completed:processID",String.valueOf(queID), getScore(jd,"prod:threads:running:processID",String.valueOf(queID)));
				jd.hdel("prod:threads:running:processID",String.valueOf(queID));
				jd.lrem("prod:threads:running", 1, tempkey);
				jd.rpush("prod:threads:completed", String.valueOf(processID) + "|" + threadStartTime + "|" + threadEndTime + "|" + command);
				System.out.println(threadEndTime + " Ending : "+ command);
			} else {
				jd.hset("prod:threads:failed:processID",String.valueOf(queID), getScore(jd,"prod:threads:running:processID",String.valueOf(queID)));
				jd.hdel("prod:threads:running:processID",String.valueOf(queID));

				jd.lrem("prod:threads:running", 1, tempkey);
				jd.rpush("prod:threads:failed", String.valueOf(processID) + "|" + threadStartTime + "|" + threadEndTime + "|" + command);
				System.out.println(threadEndTime + " Failed : "+ command);
			}


		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jd.close();
		}
	}

	@Override
	public void run() {
		//System.out.println(Thread.currentThread().getName()+' Start. Command = '+command);
		try {
			try {
				processCommand();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//System.out.println(Thread.currentThread().getName()+' End.');
	}


}
